﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using iWare.Wms.Core;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;

namespace iWare.Wms.Application
{
    /// <summary>
    /// 托盘信息
    /// </summary>
    [ApiDescriptionSettings("基础信息", Name = "WmsContainer", Order = 100)]
    [Route("api/[Controller]")]
    public class WmsContainerService : IWmsContainerService, IDynamicApiController, ITransient
    {
        private readonly IRepository<WmsContainer,MasterDbContextLocator> _wmsContainerRep;


        public WmsContainerService(
            IRepository<WmsContainer,MasterDbContextLocator> wmsContainerRep
        )
        {
            _wmsContainerRep = wmsContainerRep;
        }

        /// <summary>
        /// 分页查询托盘信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<PageResult<WmsContainerOutput>> Page([FromQuery] WmsContainerSearch input)
        {
            var wmsContainers = await _wmsContainerRep.DetachedEntities
                                     .Where(!string.IsNullOrEmpty(input.Containercode), u => EF.Functions.Like(u.ContainerCode, $"%{input.Containercode.Trim()}%"))
                                     .Where(input.Containertype != null, u => u.ContainerType == input.Containertype)
                                     .Where(input.Containerstatus != null, u => u.ContainerStatus == input.Containerstatus)
                                     .OrderBy(PageInputOrder.OrderBuilder<WmsContainerSearch>(input))
                                     .ProjectToType<WmsContainerOutput>()
                                     .ToADPagedListAsync(input.PageNo, input.PageSize);
            return wmsContainers;
        }

        /// <summary>
        /// 增加托盘信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task Add(AddWmsContainerInput input)
        {
            var isExit = await _wmsContainerRep.AnyAsync(n => n.ContainerCode == input.Containercode);
            if (isExit) throw Oops.Oh("存在的相同的托盘编码！");
            var wmsContainer = input.Adapt<WmsContainer>();
            wmsContainer.ContainerStatus = Core.Enum.ContainerStatus.KOUXIAN;
            await _wmsContainerRep.InsertAsync(wmsContainer);
        }

        /// <summary>
        /// 删除托盘信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task Delete(DeleteWmsContainerInput input)
        {
            var wmsContainer = await _wmsContainerRep.FirstOrDefaultAsync(u => u.Id == input.Id);
            if(wmsContainer.ContainerStatus!=Core.Enum.ContainerStatus.KOUXIAN) throw Oops.Oh("托盘被使用不可删除");
            await _wmsContainerRep.DeleteAsync(wmsContainer);
        }

        /// <summary>
        /// 更新托盘信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("edit")]
        public async Task Update(UpdateWmsContainerInput input)
        {
            var isExist = await _wmsContainerRep.AnyAsync(u => u.Id == input.Id, false);
            if (!isExist) throw Oops.Oh(ErrorCode.D3000);

            var wmsContainer = input.Adapt<WmsContainer>();
            await _wmsContainerRep.UpdateAsync(wmsContainer,ignoreNullValues:true);
        }

        /// <summary>
        /// 获取托盘信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<WmsContainerOutput> Get([FromQuery] QueryeWmsContainerInput input)
        {
            return (await _wmsContainerRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt<WmsContainerOutput>();
        }

        /// <summary>
        /// 获取托盘信息列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<List<WmsContainerOutput>> List([FromQuery] WmsContainerInput input)
        {
            return await _wmsContainerRep.DetachedEntities.ProjectToType<WmsContainerOutput>().ToListAsync();
        }    

    }
}
